package com.freemyleft.left.left_app.mian.index.my.Mystudent;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.recycler.MultipleItemEntity;
import com.freemyleft.left.zapp.util.LeftPreference;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 我的学生
 */
public class MyStudentDelegate extends LeftDelegate implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R2.id.recy)
    RecyclerView recyclerView;
    @BindView(R2.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private ArrayList<MultipleItemEntity> data;

    @Override
    public Object setLayout() {
        return R.layout.delegate_my_student;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        refreshLayout.setOnRefreshListener(this);
        ininData();
    }
    private void ininData() {
        String token = LeftPreference.getCustomAppProfile(TOKEN);
        String teacherid = LeftPreference.getCustomAppProfile(TEACHER_ID);
        RestClient.builder().url("/home/teacher/my_student")
                .loader(getContext())
                .params("teacherid", teacherid)
                .params("token", token)
                .params("page", 1)
                .params("limitnum", 1)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        Log.e("responnse",responnse.toString());
                        refreshLayout.setRefreshing(false);
                        data = new StudentCovert().setJsonData(responnse).convert();
                        if (data != null) {
                            data.clear();
                        }
                        MyStudentAdapter adapter = new MyStudentAdapter(data);
                        recyclerView.setAdapter(adapter);
                        LinearLayoutManager manager = new LinearLayoutManager(getContext());
                        manager.setOrientation(LinearLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(manager);
                    }
                }).buid().post();

    }

    @Override
    public void onRefresh() {
        ininData();
    }
}
